#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include "ak_drv_common.h"
#include "drv_pwm.h"

#define PERIOD_1MS  1000000

static int CyclePercent = 50;

int drv_pwm_init()
{
    int fd = -1;
    int ret = 0;
    char dev_name[64] = {0};
    char path[64] = {0};
    char str[32] = {0};

    sprintf(path, "/sys/class/pwm/pwmchip5/pwm0");

    if (!access(path, F_OK)) {
        ak_print_notice( "pwm pwmchip5 already opened.\n");
        return 0;
    }

    /*使用pwmchip5生成pwm0设备*/
    sprintf(dev_name, "/sys/class/pwm/pwmchip5/export");

    fd = open(dev_name, O_WRONLY);
    if (fd < 0) {
        ak_print_error("Open pwm/pwmchip5 fail\n");
        return -1;
    }
    ak_print_info("Create pwm/pwmchip5\n");
    
    ret = write(fd, "0", strlen("0"));
    if(ret < 0) {
        close(fd);
        ak_print_error("create pwm/pwmchip5 fail\n");
        return -1;
    }
    close(fd);

    /*初始设置占空比50%*/
    // if(set_pwm_percent(50) < 0){
    //     return -1;
    // }
    return 0;
}

int set_pwm_percent(int _cyclePercent)
{
    int fd = -1;
    int ret;
    char dev_name[64] = {0};
    char path[64] = {0};
    char str[32] = {0};
    
    /*失能pwm，以配置文件*/
    memset(path, 0, 64);
    sprintf(path, "/sys/class/pwm/pwmchip5/pwm0/enable");
    fd = open(path, O_RDWR);

    if (fd < 0) {
        ak_print_error("pwm0/enable file open failed.\n");
        return -1;
    }

    ret = write(fd, "0", strlen("0"));
    if (ret < 0) {
        ak_print_error("pwm dev set disable failed.\n");
        close(fd);
        return -1;
    }
    close(fd);

    /*设置1ms的周期*/
    memset(path, 0, 64);
    sprintf(path, "/sys/class/pwm/pwmchip5/pwm0/period");
    fd = open(path, O_RDWR);

    if (fd < 0) {
        ak_print_error("pwm0/period file open failed.\n");
        return -1;
    }

    sprintf(str, "%lld", (long long)PERIOD_1MS);      //test!!!
    ret = write(fd, str, strlen(str));
    if (ret < 0) {
        ak_print_error("pwm/pwmchip5 set period_ns failed.\n");
        close(fd);
        return -1;
    }
    ak_print_info("pwm/pwmchip5/pwm0 set period_ns!"); 
    close(fd);

    /*初始设置脉宽为cycle_lenth*/
    memset(path, 0, 64);
    sprintf(path, "/sys/class/pwm/pwmchip5/pwm0/duty_cycle");
    fd = open(path, O_RDWR);

    if (fd < 0) {
        ak_print_error("pwm0/duty_cycle file open failed.\n");
        return -1;
    }

    sprintf(str, "%lld", (long long)_cyclePercent*PERIOD_1MS/100);
    ret = write(fd, str, strlen(str));
    if (ret < 0) {
        ak_print_error("pwm/pwmchip5 set cycle failed.\n");
        close(fd);
        return -1;
    }
    CyclePercent = _cyclePercent;       //记录占空比
    ak_print_info("pwm/pwmchip5/pwm0 set %d\%!", CyclePercent); //打印设置的占空比

    close(fd);

    /*使能pwm0*/
    memset(path, 0, 64);
    sprintf(path, "/sys/class/pwm/pwmchip5/pwm0/enable");
    fd = open(path, O_RDWR);

    if (fd < 0) {
        ak_print_error("pwm/pwmchip5 enable file open failed.\n");
        return -1;
    }
    
    ret = write(fd, "1", strlen("1"));
    if (ret < 0) {
        ak_print_error("pwm/pwmchip5 set enable failed.\n");
        close(fd);
        return -1;
    }
    close(fd);

    return 0;
}

int read_cycle_percent()
{
    return CyclePercent;
}
